package leetcode.栈;

import java.util.*;

public class 比较两个数组中不同元素 {
    public static void main(String[] args) {
        Integer[] a = {0, 1, 1, 3, 4};
        Integer[] b = {0, 1, 2, 3, 5,5};

        Set c = new HashSet<>();
        for (int i = 0; i < b.length; i++) {
            c.add(b[i]);
        }

        for (int i = 0; i < a.length; i++) {
            //如果c已存在相同的元素，则temp.add（oldArray）返回false
            if (c.add(a[i])) {
                System.out.println(a[i]);
            }
        }

        List compare = compare(a, b);
        List compare1 = compare(b, a);
        System.out.println(compare);
        System.out.println(compare1);
    }

    //找出两个数组所有不同元素
    public static <T> List<T> compare(T[] t1, T[] t2) {
        List<T> list1 = Arrays.asList(t1); //将t1数组转成list数组
        List<T> list2 = new ArrayList<T>();//用来存放2个数组中不相同的元素
        for (T t : t2) {
            if (!list1.contains(t)) {
                list2.add(t);
            }
        }
        return list2;
    }
}
